<?php

namespace Partner\Controller;

use Think\Controller;
use Think\IFilter;
use Think\IReq;
use Think\ISafe;
use Think\IClient;
use Think\ITime;
use Think\Util;
use Think\Plugin;

/*
 * 商家登录
 */

class LoginController extends Controller {

    public $layout = '';

    //后台登陆
    function index() {
        $this->display();
    }

    /**
     * @brief 商家登录动作
     */
    public function login() {
        $seller_name = IFilter::act(IReq::get('username'));
        $password = IReq::get('password');
        $message = '';

        if ($seller_name == '') {
            $message = '登录名不能为空';
        } else if ($password == '') {
            $message = '密码不能为空';
        } else {
            $sellerObj = M('seller');
            $sellerRow = $sellerObj->where('seller_name = "' . $seller_name . '" and is_del = 0 and is_lock = 0')->find();
            if ($sellerRow && ($sellerRow['password'] == md5($password))) {
                $dataArray = array(
                    'id' =>  $sellerRow["id"],
                    'login_time' => ITime::getDateTime(),
                );
                $sellerObj->save($dataArray);

                //存入私密数据
                ISafe::set('seller_id', $sellerRow['id']);
                ISafe::set('seller_name', $sellerRow['seller_name']);
                ISafe::set('seller_pwd', $sellerRow['password']);

                $this->redirect("Partner/Index/index");
            } else {
                $message = '用户名与密码不匹配';
            }
        }

        if ($message != '') {
            $this->redirect("Partner/Login/index");
           // Util::showMessage($message);
        }
    }

    //后台登出
    function logout() {
        plugin::trigger('clearSeller');
        $this->redirect('index');
    }

    //加载验证码
    public function verify() {
        ob_clean();
        $Verify = new \Think\Verify();
        $Verify->fontSize = 20;
        $Verify->length = 4;
        $Verify->useNoise = false;
        $Verify->entry();
    }

}
